package jubaozhu.controller;

import jubaozhu.domain.Pic;
import jubaozhu.domain.User;
import jubaozhu.extra.base.BaseController;
import jubaozhu.extra.context.Constant;
import jubaozhu.mapper.UserMapper;
import jubaozhu.service.PicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.frame.mybatis.example.Example;
import zhibi.utils.security.SecurityUtil;

import java.util.Date;
import java.util.List;

@Controller
public class IndexController extends BaseController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PicService picService;

    @RequestMapping({"index", "/"})
    public String index(Model model) {
        //滚动图
        Example example = Example.getInstance().addParam("status", 1).addOrder("sort");
        List<Pic> picList = picService.selectByExample(example);
        model.addAttribute("picList", picList);
        return "index";
    }

    @PostMapping("login")
    public String login(String name, String password, Model model) {
        User user = new User();
        user.setPhone(name);
        user.setPassword(SecurityUtil.MD5(password).toUpperCase());
        user = userMapper.selectOne(user);
        if (null == user) {
            model.addAttribute(Constant.TIP_MSG, "手机号或密码错误");
            return "view/login";
        } else if (user.getStatus() == 0) {
            model.addAttribute(Constant.TIP_MSG, "用户被禁用");
            return "view/login";
        }
        userMapper.updateByPrimaryKeySelective(user);
        session.setAttribute(Constant.SESSION.USER, user);
        logger.info("{} 登录", user.getPhone());
        user.setLogindate(new Date());
        return redirect("index");
    }

    @PostMapping("register")
    public String register(User user, Model model) {
        User temp = new User();
        temp.setPhone(user.getPhone());
        int size = userMapper.select(temp).size();
        if (size > 0) {
            model.addAttribute(Constant.TIP_MSG, "该手机号已经被注册");
            return "view/register";
        }
        user.setAddtime(new Date());
        user.setStatus(1);
        user.setPassword(SecurityUtil.MD5(user.getPassword()));
        user.setName(user.getPhone());
        user.setLogindate(new Date());
        userMapper.insertSelective(user);
        session.setAttribute(Constant.SESSION.USER, user);
        logger.info("{} 注册", user.getPhone());
        return redirect("index");
    }

    @PostMapping("forget")
    public String forget(String phone, String password, Model model) {
        User temp = new User();
        temp.setPhone(phone);
        List<User> list = userMapper.select(temp);
        if (list.size() == 0) {
            model.addAttribute(Constant.TIP_MSG, "该手机号还没注册");
            return "view/forget";
        } else {
            User user = list.get(0);
            user.setPassword(SecurityUtil.MD5(password));
            userMapper.updateByPrimaryKeySelective(user);
        }
        return redirect("view/login");
    }


    @RequestMapping("view/{page}")
    public String view(@PathVariable String page) {
        return page;
    }


}
